package com.boc.langxiang.ui.activity.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.shop.SpecialDetailContract;
import com.boc.langxiang.entity.bean.ShareBean;
import com.boc.langxiang.entity.events.ShareEvent;
import com.boc.langxiang.entity.events.ZanEvent;
import com.boc.langxiang.entity.response.SpecialLikeContent;
import com.boc.langxiang.entity.response.SpecialTopicDetailContent;
import com.boc.langxiang.entity.response.SpecialTopicShareContent;
import com.boc.langxiang.net.H5Url;
import com.boc.langxiang.net.NetClient;
import com.boc.langxiang.presenter.shop.SpecialTopicDetailPresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.ui.dialog.SharePadDlg;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 专题详情
 */
public class SpecialTopicDetailActivity extends BaseToolBarActivity implements SpecialDetailContract.View {

    @BindView(R.id.webView)
    WebView mWebView;
    @BindView(R.id.progress_bar)
    ProgressBar mProgressBar;

    private String mId;

    private String mWebUrl;

    private SpecialDetailContract.Presenter mPresenter;

    private Menu mActionMenu;
    private String mTitle;
    private String mContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_topic_detail);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        mId = intent.getStringExtra("id");
        if (StringUtil.isEmpty(mId)) {
            finish();
            return;
        }
        mTitle = intent.getStringExtra("title");
        mContent = intent.getStringExtra("content");


        mPresenter = new SpecialTopicDetailPresenter(this);
        initViews();
    }

    private void initViews() {
        WebSettings webSettings = mWebView.getSettings();
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("url", url);
                if (url.equals(mWebUrl)) {
                    view.loadUrl(url);
                } else {
                    String replace = url.replace(H5Url.PRO_INFO, "");
                    String[] split = replace.split("&");
                    String[] product_info = split[0].split("-");
                    String id = product_info[1];
                    String[] designer_status = split[1].split("-");
                    String designer_statu = designer_status[1];

                    switch (designer_statu) {
                        case "2":
                            Intent intent = new Intent(mContext, ProductDetailActivity.class);
                            intent.putExtra("id", id);
                            startActivity(intent);
                            break;
                        case "3":
                            intent = new Intent(mContext, ProductDetail2Activity.class);
                            intent.putExtra("id", id);
                            startActivity(intent);
                            break;
                    }

                }
                return true;
            }

        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                    mProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);


    }

    private void updateUI() {
        mWebUrl = H5Url.PRO_INFO + mId;
        mWebView.loadUrl(mWebUrl);
        mPresenter.getDetail(mId);
    }

    @Override
    protected void onCreateCustomToolBar(Toolbar toolbar) {
        super.onCreateCustomToolBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.icon_return_black);
        toolbar.setBackgroundColor(Color.WHITE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_special_topic, menu);
        mActionMenu = menu;
        updateUI();
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_like:
                if (isLogin()) {
                    onLike(item);
                }
                break;
            case R.id.action_share:
                String shareUrl = NetClient.BASE_URL + "share_pro_info/" + mId;
                ShareBean shareBean = new ShareBean();
                shareBean.setTargetUrl(shareUrl);
                shareBean.setUmImage(new UMImage(mContext, R.drawable.ic_launcher));
                shareBean.setText(mContent);
                shareBean.setTitle(mTitle);
                new SharePadDlg(mContext, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ)
                        .setShareBean(shareBean)
                        .show();
//                mPresenter.getShareData(mId);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onLike(final MenuItem item) {
        mPresenter.like(mId, item);
    }

    @Override
    public void onGetDetailSuccess(SpecialTopicDetailContent content) {
        switch (content.getRes().getClick_type()) {
            case "0":
                mActionMenu.findItem(R.id.action_like).setIcon(R.drawable.icon_like_black);
                break;
            case "1":
                mActionMenu.findItem(R.id.action_like).setIcon(R.drawable.icon_like_black_solid);
                break;
        }
    }

    @Override
    public void onLikeSuccess(SpecialLikeContent content, MenuItem item) {
        EventBus.getDefault().post(new ZanEvent());
        switch (content.getRes()) {
            case "0":
                item.setIcon(R.drawable.icon_like_black);
                Toast.makeText(mContext, "已取消点赞", Toast.LENGTH_SHORT).show();
                break;
            case "1":
                item.setIcon(R.drawable.icon_like_black_solid);
                Toast.makeText(mContext, "已点赞", Toast.LENGTH_SHORT).show();
                break;
            case "2":
                onError("", "操作失败");
                break;
        }
    }

    @Override
    public void onGetShareDataSuccess(SpecialTopicShareContent content) {
//        String shareUrl = NetClient.BASE_URL + "share_pro_info/" + mId;
//        ShareBean shareBean = new ShareBean();
//        shareBean.setTargetUrl(shareUrl);
//        shareBean.setUmImage(new UMImage(mContext, R.drawable.ic_launcher));
//        shareBean.setText(content.getRes().getContent());
//        shareBean.setTitle(content.getRes().getTitle());
//        new SharePadDlg(mContext, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ)
//                .setShareBean(shareBean)
//                .show();
    }

    @Override
    public void showLoading() {
        showProgressDialog("请稍候......");
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(this, msg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShareEvent(ShareEvent event) {
        mPresenter.getShareData(mId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
